const fs = require('fs')
const path = require('path')
const ejs = require('ejs')

const fn = (tmp, dest, data) => {
  const dir = path.join(tmp, 'public')
  fs.readdir(dir, (err, files) => {
    if (err) throw err
    files.forEach(file => {
      ejs.renderFile(path.join(dir, file), data, (err, result) => {
        if (err) throw err
        const dirpath = path.join(dest, 'public')
        if (!fs.existsSync(dirpath)) {
          fs.mkdirSync(dirpath)
        }
        fs.writeFileSync(path.join(dirpath, file), result)
      })
    })
  })
}

module.exports = fn